Skip to content

fix: clean up orphaned BVH nodes when remove creates a partial root#409

Merged
sebcrozet merged 2 commits into
dimforge:masterfrom
robtfm:fix-bvh-remove-orphaned-nodes
May 28, 2026
Merged

fix: clean up orphaned BVH nodes when remove creates a partial root#409
sebcrozet merged 2 commits into
dimforge:masterfrom
robtfm:fix-bvh-remove-orphaned-nodes

Conversation

@robtfm
Copy link
Copy Markdown
Contributor

@robtfm robtfm commented Mar 24, 2026

Summary

  • When multiple Bvh::remove calls occur without an intervening refit, orphaned wide nodes accumulate in the nodes vector (by design — the comment says "it will get cleaned up at the next refit"). However, if the root is then reduced to a single leaf (partial root), those orphaned nodes remain, causing optimize_incremental to operate on a corrupt tree structure.
  • This adds a truncate(1) for both nodes and parents after creating a partial root, since only node[0] is reachable in that state.

🤖 Generated with Claude Code

@holmes-software
Copy link
Copy Markdown

Can confirm that this patch fixes my crashes:

thread 'Compute Task Pool (1)' (359437) panicked at /home/josh/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/parry3d-0.26.0/src/partitioning/bvh/bvh_traverse_bvtt.rs:44:31:
index out of bounds: the len is 1 but the index is 1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `my_app::plugins::physics::PhysicsSystems::step_rapier`!
Encountered a panic in system `bevy_app::main_schedule::FixedMain::run_fixed_main`!
Encountered a panic in system `bevy_time::fixed::run_fixed_main_schedule`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

I hope this gets merged soon.

robtfm and others added 2 commits May 28, 2026 19:09
When multiple removes occur without an intervening refit, orphaned wide
nodes accumulate in the nodes vector. If the root is then reduced to a
single leaf (partial root), those orphaned nodes remain, causing
optimize_incremental to operate on a corrupt tree and eventually crash.

Truncate nodes and parents to 1 after creating a partial root, since
only node[0] is reachable in that state.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sebcrozet sebcrozet force-pushed the fix-bvh-remove-orphaned-nodes branch from bb76d21 to 763d2a6 Compare May 28, 2026 17:10
@sebcrozet sebcrozet merged commit e606e74 into dimforge:master May 28, 2026
8 checks passed
@sebcrozet
Copy link
Copy Markdown
Member

Thanks!

@sebcrozet sebcrozet mentioned this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants